<!--
 * @Descripttion: 财务报表
 * @Author: DXV-RGWU-TUFH-RFCY-IEGMYY
 * @Date: 2021-03-15 14:28:04
 * @LastEditors: DXV-RGWU-TUFH-RFCY-IEGMYY
 * @LastEditTime: 2023-05-29 17:04:58
-->
<template>
  <div class="lb-finance-record">
    <top-nav></top-nav>
    <div class="page-main">
      <el-row class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm"
          ref="searchForm"
        >
          <el-form-item label="输入查询" prop="top_name">
            <el-input
              v-model="searchForm.top_name"
              :placeholder="placeholder"
              :style="{
                width:
                  routesItem.userInfo.is_admin ||
                  routesItem.userInfo.city_type === 3
                    ? '550px'
                    : routesItem.userInfo.city_type === 1
                    ? '500px'
                    : '460px'
              }"
            ></el-input>
          </el-form-item>
          <el-form-item label="佣金类型" prop="type">
            <el-select
              @change="getTableDataList(1)"
              v-model="searchForm.type"
              placeholder="请选择"
            >
              <el-option
                v-for="item in cashTypeList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="入账时间" prop="start_time">
            <el-date-picker
              @change="getTableDataList(1)"
              v-model="searchForm.start_time"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="timestamp"
              :picker-options="pickerOptions"
              :default-time="['00:00:00', '23:59:59']"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1)"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('searchForm')"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </el-row>
      <el-row class="page-top-operate">
        <lb-button
          size="mini"
          plain
          type="primary"
          icon="el-icon-download"
          :loading="downloadLoading"
          @click="toExportExcel"
          v-hasPermi="`${$route.name}-export`"
        >
          {{ $t('action.export') }}</lb-button
        >
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        :summary-method="getSummaries"
        show-summary
        style="width: 100%"
      >
        <el-table-column
          prop="order_code"
          min-width="150"
          label="系统订单号"
        ></el-table-column>
        <el-table-column
          prop="transaction_id"
          min-width="150"
          label="付款订单号"
        ></el-table-column>
        <el-table-column prop="apply_price" label="支付方式" min-width="120">
          <template slot-scope="scope">
            <el-tag :type="payType[scope.row.pay_model].type">{{
              payType[scope.row.pay_model].text
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="入账时间" min-width="120">
          <template slot-scope="scope">
            <p>{{ scope.row.end_time | handleTime(1) }}</p>
            <p>{{ scope.row.end_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="apply_transfer" label="财务类型" min-width="120">
          <template slot-scope="scope">
            <el-tag :type="sheetType[scope.row.is_add].type">{{
              sheetType[scope.row.is_add].text
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="true_service_price"
          label="支付金额"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ `¥${scope.row.true_service_price}` }}
          </template>
        </el-table-column>
        <el-table-column
          prop="coach_name"
          :label="$t('action.attendantName') + '名称'"
          min-width="150"
        >
        </el-table-column>
        <el-table-column
          prop="coach_cash"
          :label="$t('action.attendantName') + '提成'"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ scope.row.coach_name ? `¥${scope.row.coach_cash}` : '-' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="car_price"
          :label="$t('action.attendantName') + '车费'"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ `¥${scope.row.car_price}` }}
          </template>
        </el-table-column>
        <el-table-column
          prop="province_name"
          label="省代名称"
          min-width="150"
          v-if="
            routesItem.userInfo.is_admin ||
            (routesItem.userInfo.is_admin === 0 &&
              routesItem.userInfo.city_type === 3)
          "
        >
          <template slot-scope="scope">
            {{ scope.row.province_name || '-' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="province_cash"
          label="省代提成"
          min-width="150"
          v-if="
            routesItem.userInfo.is_admin ||
            (routesItem.userInfo.is_admin === 0 &&
              routesItem.userInfo.city_type === 3)
          "
        >
          <template slot-scope="scope">
            {{ scope.row.province_name ? `¥${scope.row.province_cash}` : '-' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="city_name"
          label="市代名称"
          min-width="150"
          v-if="
            routesItem.userInfo.is_admin ||
            (routesItem.userInfo.is_admin === 0 &&
              [1, 3].includes(routesItem.userInfo.city_type))
          "
        >
          <template slot-scope="scope">
            {{ scope.row.city_name || '-' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="city_cash"
          label="市代提成"
          min-width="150"
          v-if="
            routesItem.userInfo.is_admin ||
            (routesItem.userInfo.is_admin === 0 &&
              [1, 3].includes(routesItem.userInfo.city_type))
          "
        >
          <template slot-scope="scope">
            {{ scope.row.city_name ? `¥${scope.row.city_cash}` : '-' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="district_name"
          label="区县代理名称"
          min-width="150"
          v-if="
            routesItem.userInfo.is_admin ||
            (routesItem.userInfo.is_admin === 0 &&
              [1, 2, 3].includes(routesItem.userInfo.city_type))
          "
        >
          <template slot-scope="scope">
            {{ scope.row.district_name || '-' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="district_cash"
          label="区县代理提成"
          min-width="150"
          v-if="
            routesItem.userInfo.is_admin ||
            (routesItem.userInfo.is_admin === 0 &&
              [1, 2, 3].includes(routesItem.userInfo.city_type))
          "
        >
          <template slot-scope="scope">
            {{ scope.row.district_name ? `¥${scope.row.district_cash}` : '-' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="cash_user_name"
          label="分销合伙人姓名"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ scope.row.cash_user_name || '-' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="user_cash"
          label="分销合伙人提成"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ scope.row.cash_user_name ? `¥${scope.row.user_cash}` : '-' }}
          </template>
        </el-table-column>
        <el-table-column prop="partner_name" label="经纪人姓名" min-width="150">
          <template slot-scope="scope">
            {{ scope.row.partner_name || '-' }}
          </template>
        </el-table-column>
        <el-table-column prop="partner_cash" label="经纪人提成" min-width="150">
          <template slot-scope="scope">
            {{ scope.row.partner_name ? `¥${scope.row.partner_cash}` : '-' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="salesman_name"
          label="业务员姓名"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ scope.row.salesman_name || '-' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="salesman_cash"
          label="业务员提成"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ scope.row.salesman_name ? `¥${scope.row.salesman_cash}` : '-' }}
          </template>
        </el-table-column>
        <el-table-column prop="channel_name" label="渠道商姓名" min-width="150">
          <template slot-scope="scope">
            {{ scope.row.channel_name || '-' }}
          </template>
        </el-table-column>
        <el-table-column prop="channel_cash" label="渠道商提成" min-width="150">
          <template slot-scope="scope">
            {{ scope.row.channel_name ? `¥${scope.row.channel_cash}` : '-' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="remain_cash"
          label="平台利润"
          min-width="150"
          v-if="routesItem.userInfo.is_admin"
        >
          <template slot-scope="scope">
            {{ `¥${scope.row.remain_cash}` }}
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
  </div>
</template>

<script>
import moment from 'moment'
import { mapState } from 'vuex'
export default {
  data () {
    return {
      placeholder: '',
      cashTypeList: [{ label: '全部', value: 0 }, { label: '分销合伙人', value: 1 }, { label: '代理商', value: 2 }, { label: this.$t('action.attendantName') + '', value: 3 }, { label: '车费', value: 8 }, { label: '经纪人', value: 9 }, { label: '渠道商', value: 10 }, { label: '业务员', value: 12 }],
      payType: {
        1: { type: 'warning', text: '微信支付' },
        2: { type: 'primary', text: '余额支付' },
        3: { type: 'success', text: '支付宝支付' }
      },
      sheetType: {
        0: { type: 'warning', text: '销售' },
        1: { type: 'primary', text: '加钟' }
      },
      searchForm: {
        page: 1,
        limit: 10,
        top_name: '',
        start_time: '',
        end_time: '',
        type: 0
      },
      loading: false,
      total: 0,
      tableData: [],
      total_list: {},
      downloadLoading: false
    }
  },
  created () {
    let { is_admin: admin, city_type: ctype } = this.routesItem.userInfo
    let cityText = admin || ctype === 3 ? '/省代理/市代理/区县代理' : ctype === 1 ? '/市代理/区县代理' : '/区县代理'
    this.placeholder = `请输入${this.$t('action.attendantName')}${cityText}/分销合伙人/经纪人/业务员/渠道商名称`
    this.getTableDataList()
  },
  computed: {
    ...mapState({
      routesItem: state => state.routes
    })
  },
  methods: {
    resetForm (form) {
      this.$refs[form].resetFields()
      this.getTableDataList(1)
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    async getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.tableData = []
      this.loading = true
      let searchForm = JSON.parse(JSON.stringify(this.searchForm))
      let { start_time: time } = searchForm
      if (time && time.length > 0) {
        searchForm.start_time = time[0] / 1000
        searchForm.end_time = time[1] / 1000
      } else {
        searchForm.start_time = ''
        searchForm.end_time = ''
      }
      let { code, data } = await this.$api.finance.financeDetailedList(searchForm)
      this.loading = false
      if (code !== 200) return
      this.tableData = data.data
      this.total = data.total
      this.total_list = data.total_list
    },
    // 求和操作
    getSummaries (param) {
      const { columns, data } = param
      const sums = []
      let arr = ['true_service_price', 'coach_cash', 'car_price', 'province_cash', 'city_cash', 'district_cash', 'user_cash', 'partner_cash', 'salesman_cash', 'channel_cash', 'remain_cash']
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计:'
        }
        // 求和
        else if (arr.includes(column.property)) {
          sums[index] = `¥` + this.total_list[column.property]
        }
      })
      return sums
    },
    /**
     * @method 导出
     */
    toExportExcel () {
      this.downloadLoading = true
      let searchForm = JSON.parse(JSON.stringify(this.searchForm))
      let { start_time: time } = searchForm
      if (time && time.length > 0) {
        searchForm.start_time = time[0] / 1000
        searchForm.end_time = time[1] / 1000
      } else {
        searchForm.start_time = ''
        searchForm.end_time = ''
      }
      let url = this.$util.getProCurrentHref()
      let keywords = url.indexOf('?') > 0 ? '' : '?'
      let flag = url.indexOf('?') > 0
      Object.getOwnPropertyNames(searchForm).forEach((keys, value) => {
        keywords += flag
          ? `&${keys}=${searchForm[keys]}`
          : `${keys}=${searchForm[keys]}`
        flag = true
      })
      let token = window.localStorage.getItem('massage_minitk')
      let dwonlaodUrl = `${url}/massage/admin/AdminExcel/financeDetailedList${keywords}&token=${token}`
      window.location.href = dwonlaodUrl
      setTimeout(() => {
        this.downloadLoading = false
      }, 5000)
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }

}
</script>

<style lang="scss" scoped>
.lb-finance-record {
  width: 100%;
}
</style>
